# -*-mode: Makefile-*-
# Make.target for GENIE Neutrino Generator
#
# Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
#

#
#
PACKAGE_LIB                   = $(LIBNAME)$(GLIBVERSION).$(DllSuf)
PACKAGE_LIB_WITH_PATH         = $(GENIE_LIB_PATH)/$(LIBNAME)$(GLIBVERSION).$(DllSuf)
PACKAGE_LIBNOVRS              = $(LIBNAME).$(DllSuf)
PACKAGE_LIBNOVRS_WITH_PATH    = $(GENIE_LIB_PATH)/$(LIBNAME).$(DllSuf)
PACKAGE_LIBMACLINK_WITH_PATH  = $(GENIE_LIB_PATH)/$(LIBNAME).$(DllLinkSuf)
PACKAGE_LIBNOVRS_WITH_IPATH   = $(GENIE_LIB_INSTALLATION_PATH)/$(LIBNAME).$(DllSuf)
PACKAGE_LIBMACLINK_WITH_IPATH = $(GENIE_LIB_INSTALLATION_PATH)/$(LIBNAME).$(DllLinkSuf)

PACKAGE_ALL_SOURCES          := $(wildcard *.cxx)
DICTGEN_HEADERS              := $(addsuffix .h, $(basename $(PACKAGE_ALL_SOURCES)))
DICTGEN_OBJECTS              := $(addsuffix .$(ObjSuf), $(basename $(DICTGEN_HEADERS)))
PACKAGE_ALL_SOURCES          := $(PACKAGE_ALL_SOURCES) $(DICTIONARY).cc $(wildcard *.cpp)
PACKAGE_ALL_OBJECTS           = $(addsuffix .$(ObjSuf), $(basename $(PACKAGE_ALL_SOURCES)))
PACKAGE_SOURCES              := $(wildcard *.cxx)
PACKAGE_OBJECTS               = $(addsuffix .$(ObjSuf), $(basename $(PACKAGE_SOURCES)))

SUBDIR_HEADERS_H              = $(wildcard *.h)
SUBDIR_HEADERS_ICC            = $(wildcard *.icc)
SUBDIR_HEADERS                = $(SUBDIR_HEADERS_H) $(SUBDIR_HEADERS_ICC)
### pcm & rootmap files should follow library naming ??
#DICTIONARY_PCM               := $(DICTIONARY)_rdict.pcm
#DICTIONARY_PCM_WITH_PATH     := $(GENIE_LIB_PATH)/$(DICTIONARY)_rdict.pcm
#DICTIONARY_ROOTMAP           := $(DICTIONARY).rootmap
#DICTIONARY_ROOTMAP_WITH_PATH := $(GENIE_LIB_PATH)/$(DICTIONARY).rootmap

PACKAGE_PCM               := $(LIBNAME)_rdict.pcm
PACKAGE_PCM_WITH_PATH     := $(GENIE_LIB_PATH)/$(PACKAGE_PCM)
PACKAGE_ROOTMAP           := $(LIBNAME).rootmap
PACKAGE_ROOTMAP_WITH_PATH := $(GENIE_LIB_PATH)/$(PACKAGE_ROOTMAP)

LIB_DEPEND=
ifeq ($(strip $(DICTIONARY)),undefined)
  LIB_DEPEND = $(PACKAGE_OBJECTS)
else
  LIB_DEPEND = $(PACKAGE_ALL_OBJECTS)
endif

#
#
rootcint: $(DICTIONARY).o

$(DICTIONARY).cc: LinkDef.h $(DICTGEN_OBJECTS)
	$(RM) $(DICTIONARY).*
	@echo "[package: $(PACKAGE)] Generating ROOT dictionary ..."
#	rootcint -f $(DICTIONARY).cc -c $(ROOT_DICT_GEN_INCLUDES) $(DICTGEN_HEADERS) LinkDef.h
ifeq ($(ROOT_MAJOR),5)
        # add -p flag to use cpp rather than CINT
	  rootcint -f $(DICTIONARY).cc -c -p $(ROOT_DICT_GEN_INCLUDES) $(DICTGEN_HEADERS) LinkDef.h
else
        # to make pcm relocatable we have to set it to strip these off
        # sub-dirs Types & Interfaces don't have dictionaries, made as part of Nuclear
        # so we have to explicitly (in case of Nuclear, no harm doing for rest)
        # set include path to those
        # lots of possible include paths to try to catch all cases of in situ, installed
        # and re-packages into UPS
	  ROOT_INCLUDE_PATH=$(GENIE_LIB_PATH):$(GENIE)/src:$(GENIE)/src/$(PACKAGE) \
          rootcling -f $(DICTIONARY).cc \
                    -I$(PACKAGE) -IGENIE \
                    -IGENIE/$(PACKAGE) \
                    -I../include -I../include/GENIE \
                    -I../include/GENIE/$(PACKAGE) \
                    -inlineInputHeader \
                    -s $(PACKAGE_LIBNOVRS) \
                  -rml $(PACKAGE_PCM) \
                  -rmf $(PACKAGE_ROOTMAP) \
                  $(ROOT_DICT_GEN_INCLUDES) $(DICTGEN_HEADERS) LinkDef.h
# clean rootmap output up a bit ... (magic?)
# -e "s/\.dylib\.so/\.dylib/g"
#   order matters here
	  sed -i.bak \
	             -e "s/_rdict.pcm/.so/g"  \
	             -e "s%^header *%header $(PACKAGE)/%g" \
	             -e "s%$(GENIE_LIB_PATH)/%%g" \
	     $(PACKAGE_ROOTMAP)
endif

#
#

$(PACKAGE_LIB_WITH_PATH): $(LIB_DEPEND)
	@echo "[package: $(PACKAGE)] Generating shared library ..dictionary=.$(DICTIONARY)"
ifeq ($(strip $(DICTIONARY)),undefined)
	$(SOCMD) $(SOFLAGS) $(SOMINF) $(PACKAGE_OBJECTS) $(EXTRA_EXT_LIBS) $(OutPutOpt) $(PACKAGE_LIB_WITH_PATH)
else
	$(SOCMD) $(SOFLAGS) $(SOMINF) $(PACKAGE_ALL_OBJECTS) $(EXTRA_EXT_LIBS) $(OutPutOpt) $(PACKAGE_LIB_WITH_PATH)
#	[ ! -f $(DICTIONARY_PCM) ] || $(COPY) $(DICTIONARY_PCM) $(GENIE_LIB_PATH)
#	[ ! -f $(DICTIONARY_ROOTMAP) ] || $(COPY) $(DICTIONARY_ROOTMAP) $(GENIE_LIB_PATH)
	[ ! -f $(PACKAGE_PCM) ] || $(COPY) $(PACKAGE_PCM) $(GENIE_LIB_PATH)
	[ ! -f $(PACKAGE_ROOTMAP) ] || $(COPY) $(PACKAGE_ROOTMAP) $(GENIE_LIB_PATH)
endif

lib: $(PACKAGE_LIB_WITH_PATH)

$(PACKAGE_LIBMACLINK_WITH_PATH): $(PACKAGE_LIB_WITH_PATH)
ifeq ($(strip $(IS_MACOSX)),YES)
	$(SYMLINK) $(PACKAGE_LIB) $(PACKAGE_LIBMACLINK_WITH_PATH)
endif


$(PACKAGE_LIBNOVRS_WITH_PATH): $(PACKAGE_LIB_WITH_PATH)
	@echo "[package: $(PACKAGE)] Creating symbolic link to shared library ..."
ifeq ($(strip $(GOPT_ENABLE_DYLIBVERSION)),YES)
	$(SYMLINK) $(PACKAGE_LIB) $(PACKAGE_LIBNOVRS_WITH_PATH)
endif

#
#
lib-link: $(PACKAGE_LIBNOVRS_WITH_PATH) $(PACKAGE_LIBMACLINK_WITH_PATH)


#
#
install-lib:
	@echo "[package: $(PACKAGE)] Installing library..."
	[ ! -f $(PACKAGE_LIB_WITH_PATH) ] || $(COPY) $(PACKAGE_LIB_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
#	[ ! -f $(DICTIONARY_PCM_WITH_PATH) ] || $(COPY) $(DICTIONARY_PCM_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
#	[ ! -f $(DICTIONARY_ROOTMAP_WITH_PATH) ] || $(COPY) $(DICTIONARY_ROOTMAP_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
	[ ! -f $(PACKAGE_PCM_WITH_PATH) ] || $(COPY) $(PACKAGE_PCM_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
	[ ! -f $(PACKAGE_ROOTMAP_WITH_PATH) ] || $(COPY) $(PACKAGE_ROOTMAP_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)

ifeq ($(strip $(GOPT_ENABLE_DYLIBVERSION)),YES)
	[ ! -f $(PACKAGE_LIB_WITH_PATH) ] || $(SYMLINK) $(PACKAGE_LIB) $(PACKAGE_LIBNOVRS_WITH_IPATH)
endif
ifeq ($(ROOT_MAJOR),6)
	[ ! -f $(PACKAGE_PCM_WITH_PATH) ]     || $(COPY) $(PACKAGE_PCM_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
	[ ! -f $(PACKAGE_ROOTMAP_WITH_PATH) ] || $(COPY) $(PACKAGE_ROOTMAP_WITH_PATH) $(GENIE_LIB_INSTALLATION_PATH)
endif
ifeq ($(strip $(ARCH)),macosx)
	[ ! -f $(PACKAGE_LIB_WITH_PATH) ] || $(SYMLINK) $(PACKAGE_LIB) $(PACKAGE_LIBMACLINK_WITH_IPATH)
endif

#
#
install-inc:
	@echo "[package: $(PACKAGE)] Installing headers..."
	$(COPY) $(SUBDIR_HEADERS) $(GENIE_INC_INSTALLATION_PATH)/$(PACKAGE)

#
#
.PHONY: purge
purge:
	$(RM) -f $(PACKAGE_OBJECTS) *.d *~ core a.out Makefile.temp


#
#
.PHONY: clean
clean:
	@echo "Clean in $(PACKAGE)"
	$(RM) -f *.rootmap *.rootmap.bak *.pcm *.d *.o *~ core a.out Makefile.temp
ifneq ($(strip $(DICTIONARY)),undefined)
	$(RM) -f $(DICTIONARY)* $(GENIE_LIB_PATH)/$(DICTIONARY)* 
endif
ifneq ($(strip $(LIBNAME)),undefined)
	$(RM) -f $(LIBNAME)* $(GENIE_LIB_PATH)/$(LIBNAME)*
endif


#
#
.PHONY: distclean
distclean:
ifneq ($(strip $(LIBNAME)),undefined)
	$(RM) -f $(GENIE_LIB_INSTALLATION_PATH)/$(LIBNAME)*
endif


DEP=$(addsuffix .d, $(basename $(PACKAGE_ALL_SOURCES)))

-include $(DEP)
